//
//  NewDetailViewController.m
//  CarWorld
//
//  Created by 曹杨杨 on 15/3/28.
//  Copyright (c) 2015年 CarWorldTeam. All rights reserved.
//

#import "NewDetailViewController.h"
#import "newsWebViewData.h"
@interface NewDetailViewController ()
@property (nonatomic,retain) NSMutableArray *urlArray;
@property (nonatomic,retain) NSString *strUrl;
@end

@implementation NewDetailViewController

- (void)parisData:(NSData *)data
{
    if (data) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.urlArray = [NSMutableArray array];
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
            NSDictionary *dic1 = dic[@"RESULT"];
//            for (NSString *key in dic1) {
//            NSLog(@"!!!!!%@",dic1);
                NSArray *arr = dic1[@"sub_pages"];
//            NSLog(@"----%@   -- ",dic1[@"sub_pages"]);
//                for (NSDictionary *dic2 in arr) {
                    newsWebViewData *cellData = [[newsWebViewData alloc] init];
                    [cellData setValuesForKeysWithDictionary:arr[0]];
                    [_urlArray addObject:cellData];
                    [cellData release];
//                    NSLog(@"%@",cellData);
//                }
            _strUrl = [NSString stringWithFormat:@"%@",[_urlArray objectAtIndex:0]];
            [self webView:_strUrl];
        });
        
    }
}

- (void)webView:(NSString *)str
{
    CGRect rect = [UIScreen mainScreen].bounds;
    CGFloat width = rect.size.width;
    CGFloat height = rect.size.height - 64 - 44;
    UIWebView *priceView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, width, height)];
    priceView.delegate = self;
    [self.view addSubview:priceView];
    
    priceView.scalesPageToFit = YES;
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
    [priceView loadRequest:request];
    [priceView release];
}

- (void)getNewURL
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://mobile.auto.sohu.com/mcms/external/queryNewsDetail.at?articleId=%@&storeType=1",_newsId]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (data) {
            [self parisData:data];
        }
    }];
}
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"详情";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"biaoqing_h@3x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(back)];
//    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:41/255.0 green:129/255.0 blue:255/255.0 alpha:1];
    UIView *view1 = [[UIView alloc] init];
    view1.frame = self.view.bounds;
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    [self getNewURL];
//    UIWebView *priceView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    priceView.delegate = self;
//    [self.view addSubview:priceView];
//    
//    priceView.scalesPageToFit = YES;
//    
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://auto.sohu.com/mapp/newsfile/app/20150303/409307957_1_new.html"]];
//    [priceView loadRequest:request];
//    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
